﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BFTest
{
    class CMessageList
    {
        List<CMessage> messages = new List<CMessage>();
        public List<CMessage> Messages { get { return messages; } private set { messages = value; } }

        int maxMessages;
        public int MaxMessages { get { return maxMessages; } private set { maxMessages = value; } }

        public CMessageList(int maxMessages = 0)
        {
            MaxMessages = maxMessages;
        }

        public void Draw(SpriteBatch spriteBatch, Vector2 position, SpriteFont font, Color color)
        {
            if (MaxMessages == 0)
                throw new Exception("Calling this draw method on an unending list probably isn't wanted.");
            else
            {
                if (Messages.Count > 0)
                {
                    string messages = "";
                    foreach (CMessage message in Messages)
                    {
                        messages += message.Text + "\n";
                    }

                    spriteBatch.DrawString(font, messages, position, color);
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch, Vector2 position, SpriteFont font, Color color, int quantity)
        {
            if (Messages.Count > 0)
            {
                string messages = "";
                if (quantity > Messages.Count)
                    quantity = Messages.Count;
                for (int x = quantity; x > 0; x--)
                {
                    messages += Messages[Messages.Count - x].Text + "\n";
                }
             
                spriteBatch.DrawString(font, messages, position, color);
            }
        }

        public void Add(string text)
        {
            if(MaxMessages > 0 && Messages.Count == MaxMessages)
            {
                Messages.RemoveAt(0);
            }

            Messages.Add(new CMessage(text));
        }
    }
}
